/****************************************************************************

	COPYRIGHT(C) MAXSI SOFTWARE, JONAS 'SORTIE' TERMANSEN 2008, 2009, 2010

    This file is part of Maxsi Compile.

    Maxsi Compile is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Maxsi Compile is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Maxsi Compile.  If not, see <http://www.gnu.org/licenses/>.

	*/ #include "MaxsiCompileContributors.h" /*

	If you modify this file, please enter your name below and provide contact
	information in MaxsiCompileContributors.h. For more information please see
	MaxsiCompileContributors.h.
	
	Contributors to this file:

	- Jonas 'Sortie' Termansen
	- [your name here]


	MaxsiCompile.exe
	A system to control MaxsiCompile Systems.
	
	Main.cpp
	Basic WIN32 functions and stuff

****************************************************************************/

#include "MaxsiEngine.h"
#include <io.h>
#include <direct.h>
#include <iostream> 
#include <fcntl.h>
#include <SYS\Stat.h>
#include "Main.h"
#include "UI.h"
#include "CompileMap.h"

ME_WIN32ENTRYPOINT(MaxsiCompile);

/****************************************************************************

    FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
    PURPOSE: calls initialization function, processes message loop

****************************************************************************/

int WINAPI MaxsiCompile::WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow)
{

	//MESTR*		FinalTest		=	StringReplace(
	//	_MESTR("%BSP%"),
	//	_MESTR("c:\\program files (x86)\\steam\\steamapps\\sortie\\sourcesdk\\bin\\orangebox\\bin\\vbsp.exe"),
	//	BuildString(1,_MESTR("\"%BSP%\" -game \"%GAMEDIR%\" \"%VMFNOEXT%\"")));

	ModMeta*			Mod				=	new ModMeta;
	MapMeta*			Map				=	new MapMeta;
	Configuration*		Config			=	new Configuration;
	ToolConfiguration**	Tools			=	new ToolConfiguration*[4];	
	bool				AllocSuccess	=	( Mod && Map && Config && Tools );

	if ( AllocSuccess )
	{
		for ( size_t I = 0; I < 4; I++ )
		{
			Tools[I]				=	new ToolConfiguration;
			if ( !Tools[I] ) { AllocSuccess = false; }
		}
	}

	SteamDir			=	_MESTR("C:\\Program Files (x86)\\Steam");
	SteamUsername		=	_MESTR("Sortie");


	if ( AllocSuccess )
	{
		Mod->BSP					=	BuildString(1,_MESTR("c:\\program files (x86)\\steam\\steamapps\\sortie\\sourcesdk\\bin\\orangebox\\bin\\vbsp.exe"));
		Mod->Light					=	BuildString(1,_MESTR("c:\\program files (x86)\\steam\\steamapps\\sortie\\sourcesdk\\bin\\orangebox\\bin\\vrad.exe"));
		Mod->Vis					=	BuildString(1,_MESTR("c:\\program files (x86)\\steam\\steamapps\\sortie\\sourcesdk\\bin\\orangebox\\bin\\vvis.exe"));
		Mod->MapDir					=	BuildString(1,_MESTR("c:\\program files (x86)\\steam\\steamapps\\SourceMods\\Altered Transmission_dev\\mapsrc"));
		Mod->BSPDir					=	BuildString(1,_MESTR("c:\\program files (x86)\\steam\\steamapps\\SourceMods\\Altered Transmission_dev\\maps"));
		Mod->GameDir				=	BuildString(1,_MESTR("c:\\program files (x86)\\steam\\steamapps\\SourceMods\\Altered Transmission_dev\\SDK2007"));
		Mod->GameExe				=	BuildString(1,_MESTR("c:\\program files (x86)\\steam\\steamapps\\sortie\\half-life 2 episode two\\hl2.exe"));
		Mod->GameExeDir				=	BuildString(1,_MESTR("c:\\program files (x86)\\steam\\steamapps\\sortie\\half-life 2 episode two"));
		
		Mod->SDKEngineVersion		=	BuildString(1,_MESTR("OrangeBox"));
		Mod->SDKEngineRoot			=	BuildString(7, SteamDir, "\\SteamApps", "\\", SteamUsername, "\\SourceSDK\\bin", "\\", Mod->SDKEngineVersion);

		Map->Mod					=	Mod;
		Map->FileName				=	BuildString(3,Mod->MapDir,"\\","C12_SECTOR_O_01.vmf");

		Config->Configurations		=	Tools; // Config is now responsible for cleaning this up
		Config->NumConfigurations	=	4;

		Tools[0]->Executable			=	BuildString(1,_MESTR("%BSP%"));
		Tools[0]->AdditionalArguments	=	BuildString(1,_MESTR("-game \"%GAMEDIR%\" \"%VMFNOEXT%\""));

		Tools[1]->Executable			=	BuildString(1,_MESTR("%VIS%"));
		Tools[1]->AdditionalArguments	=	BuildString(1,_MESTR("-game \"%GAMEDIR%\" \"%VMFNOEXT%\""));

		Tools[2]->Executable			=	BuildString(1,_MESTR("%LIGHT%"));
		Tools[2]->AdditionalArguments	=	BuildString(1,_MESTR("-ldr -game \"%GAMEDIR%\" \"%VMFNOEXT%\""));

		Tools[3]->Executable			=	BuildString(1,_MESTR("%LIGHT%"));
		Tools[3]->AdditionalArguments	=	BuildString(1,_MESTR("-hdr -game \"%GAMEDIR%\" \"%VMFNOEXT%\""));


		CompileRequest*		Request		=	CreateCompileRequest(Map, Config);

		if ( Request )
		{
			//int		CompileResult		=	CompileMap(Request);
		}

		IFDELSINGLE(Request);
	}
	IFDELSINGLE(Mod);
	IFDELSINGLE(Map);
	IFDELSINGLE(Config);

	//CompileMap(_MESTR("C12_SECTOR_O_01"), _MESTR("-low"), _MESTR("-low"), _MESTR("-low -ldr"), _MESTR("-low -hdr"));

	int Result = BaseClass::WinMain(hInstance,hPrevInstance,lpCmdLine,nCmdShow);

	// "Its power is recursive. Think of a computer program. Somewhere, there is one key instruction,
	// and everything else is just functions calling themselves, or brackets billowing out endlessly
	// through an infinite address space. What happens when the brackets collapse?
	// Where's the final 'end if'? Is any of this making sense? Arthur?"

	return Result;
}


/****************************************************************************

    FUNCTION: InitApplication(HANDLE)
    PURPOSE: Initializes window data and registers window class

****************************************************************************/

BOOL MaxsiCompile::InitApplication(
    HANDLE hInstance)
{
    WNDCLASS  wc = {0};

    wc.lpfnWndProc = (WNDPROC) MaxsiCompileProc;
    wc.hInstance = (HINSTANCE)hInstance;
    wc.hIcon = LoadIconA((HINSTANCE)hInstance, "ProductIcon");
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
    wc.lpszClassName = TEXT("MaxsiCompile");

	if (!RegisterClass(&wc)) { return FALSE; }

	return TRUE;
}


/****************************************************************************

    FUNCTION:  InitInstance(HANDLE, int)
    PURPOSE:  Saves instance handle and creates main window

****************************************************************************/
BOOL MaxsiCompile::InitInstance(
    HINSTANCE hInstance, 
    INT nCmdShow)
{
    LPTSTR lpszText = NULL;
    BOOL bSuccess = TRUE;

    hInst = hInstance;

    hAccTable = LoadAccelerators(hInst, TEXT("MaxsiCompile"));

	// Get the screen size
	DEVMODE Dev;
	BOOL	Result	=	EnumDisplaySettings(NULL,ENUM_CURRENT_SETTINGS,&Dev);
	int w = 800;
	int h = 600;
	int x = (Result) ? (int)(Dev.dmPelsWidth - w ) / 2 : (int)CW_USEDEFAULT;
	int y = (Result) ? (int)(Dev.dmPelsHeight - h ) / 2 : (int)CW_USEDEFAULT;

	MESTR*	WindowTitle		=	BuildString(1,_MESTR("MaxsiCompile"));

    WindowHWND = CreateWindowM(
        _MESTR("MaxsiCompile"),
        WindowTitle,
        (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX ),
        x, y,
        w, h,
        NULL, NULL, hInstance, NULL );

	delete[] WindowTitle;

	if (!WindowHWND) { return FALSE; }


	ME_Window*	Parent	=	ME_WindowGetPointer(WindowHWND);

	Form = InitializeForm(new MaxsiCompileUI, Parent);

	ShowWindow(WindowHWND, nCmdShow);
    bSuccess = UpdateWindow(WindowHWND);

	//char* OutBuffer		=	new char[1024*1024];
	
	//for ( size_t N = 0; N < 1024*1024; N++ )
	//{
	//	if ( N % 2 == 0 )
	//	{
	//		OutBuffer[N] = 'L';
	//	}
	//	else
	//	{
	//		OutBuffer[N] = 'O';
	//	}
	//}

	//SyncWriteFile("C:\\MaxsiCompileTest.txt",OutBuffer,1024*1024,0);

	return bSuccess;
}

/****************************************************************************

    FUNCTION: MainWndProc(HWND, unsigned, WORD, LONG)
    PURPOSE:  Processes messages

****************************************************************************/

LRESULT APIENTRY MaxsiCompile::WindowProc(
    HWND hWnd, 
    UINT message, 
    WPARAM wParam, 
    LPARAM lParam)
{
	return BaseClass::WindowProc(hWnd,message,wParam,lParam);
}


DWORD MaxsiCompile::OnThreadRegistered(HWND hWnd)
{
	return BaseClass::OnThreadRegistered(hWnd);
}